package com.example.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.myapplication.PaperInfoActivity;
import com.example.myapplication.R;
import com.example.myapplication.TeacherInfoActivity;
import com.example.myapplication.bean.WorkInfoemation;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/15.
 */
public class WorkRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<WorkInfoemation> list = new ArrayList<>();
    private LayoutInflater mInflater;
    BitmapUtils bitmapUtils;
    private Context context;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private int totalSize;

    @Override
    public int getItemViewType(int position) {
        if(list.size()>0) {
            return list.get(position).getType();
        }
        return 0;
    }


    public WorkRecyclerAdapter(Context context,List<WorkInfoemation> list){
        this.list = list;
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType==0) {
            View view = mInflater.inflate(R.layout.list_work_recycleview, parent, false);
            WorkHolder workHolder = new WorkHolder(view,onClick);
            bitmapUtils = new BitmapUtils(context);
            return workHolder;
        }else if(viewType==1) {
            View view = mInflater.inflate(R.layout.footer, parent, false);
            Footer footer = new Footer(view);
            return footer;
        }
        return null;
    }

    viewHolderClickListener onClick = new viewHolderClickListener() {
        @Override
        public void onContextListen(WorkInfoemation workInfo, int position) {
            Log.i("kskks",workInfo.getWorkid());
            Log.i("kskksks",position+"");
            String workid = workInfo.getWorkid();
            Intent intent = new Intent();
            intent.setClass(context,PaperInfoActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("workid",workid);
            intent.putExtras(bundle);
            context.startActivity(intent);
        }

        @Override
        public void onUserClickListen(WorkInfoemation workInfo, int position) {

            String teacher_id = workInfo.getTeacher_id();
            String teacher_number =workInfo.getTeacher_number();
            Intent intent = new Intent();
            intent.setClass(context, TeacherInfoActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("teacher",workInfo);
            intent.putExtras(bundle);
            context.startActivity(intent);
        }
    };
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof WorkHolder) {
            ((WorkHolder)holder).teacher_name.setText(list.get(position).getTeacher_name());

            ((WorkHolder)holder).content.setText(list.get(position).getWorkinfo());
            ((WorkHolder)holder).time.setText(list.get(position).getDate());
            ((WorkHolder)holder).title.setText(list.get(position).getWorkname());
            // 加载网络图片
            bitmapUtils.display( ((WorkHolder)holder).Iv_teacher, "http://192.168.191.1/app/pic" + list.get(position).getTeacher_pic());
            WorkInfoemation workInfo = list.get(position);
            ((WorkHolder)holder).bind(workInfo);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void changeList(List<WorkInfoemation> list) {
        notifyDataSetChanged();
    }

    public class WorkHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView title, time, teacher_name,content;
        private CircularImageView Iv_teacher;
        private LinearLayout linear_info,linear_context;
        private viewHolderClickListener mlistener;
        private CardView cardView ;

        public WorkHolder(View view,viewHolderClickListener viewHolderClickListener) {
            super(view);
            mlistener =  viewHolderClickListener;
            title = (TextView) view.findViewById(R.id.title);
            content = (TextView) view.findViewById(R.id.content);
            time = (TextView) view.findViewById(R.id.time);
            cardView = (CardView) view.findViewById(R.id.cardview);
            teacher_name = (TextView) view.findViewById(R.id.teacher_name);
            Iv_teacher = (CircularImageView) view.findViewById(R.id.Iv_teacher);
            linear_context = (LinearLayout) view.findViewById(R.id.linear_context);
            linear_info = (LinearLayout) view.findViewById(R.id.linear_info);

            teacher_name.setOnClickListener(this);
            cardView.setOnClickListener(this);
            Iv_teacher.setOnClickListener(this);
        }

        public void bind(WorkInfoemation workInfo) {
            linear_info.setTag(workInfo);
            linear_context.setTag(workInfo);
            teacher_name.setTag(workInfo);
            Iv_teacher.setTag(workInfo);
            cardView.setTag(workInfo);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.cardview:
                    mlistener.onContextListen((WorkInfoemation) linear_info.getTag(),getLayoutPosition());
                    break;
                case R.id.teacher_name:
                    mlistener.onUserClickListen((WorkInfoemation) linear_context.getTag(),getLayoutPosition());
                    break;
                case R.id.Iv_teacher:
                    mlistener.onUserClickListen((WorkInfoemation) linear_context.getTag(),getLayoutPosition());
            }
        }
    }
    public interface viewHolderClickListener{
        void onContextListen(WorkInfoemation workInfo,int position);
        void onUserClickListen(WorkInfoemation workInfo,int position);
    }

    public class Footer extends RecyclerView.ViewHolder{

        public Footer(View itemView) {
            super(itemView);
        }
    }
}
